package handler

import (
	"reg-sys/model"

	"github.com/gin-gonic/gin"
)

func AddArticle(c *gin.Context) {

	var Title = c.PostForm("title")
	var Author = c.PostForm("author")
	var Desc = c.PostForm("desc")

	if Title == "" || Author == "" || Desc == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "标题，作者，描述不能为空",
		})
		return
	}

	var a model.Article
	a.Title = Title
	a.Author = Author
	a.Desc = Desc

	err := model.DB.Create(&a).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})

}

func List(c *gin.Context) {

	var article []model.Article

	err := model.DB.Find(&article).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "获取列表失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取列表成功",
		"data": article,
	})
	return

}
